package cdc.cs.common;

import cdc.cs.config.ODataConfig;
import cdc.cs.entity.SuppliersVO;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @ClassName: HttpWithAuthService
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:37
 * @Version: 1.0
 */
public class HttpWithAuthService {
    /**
     * get请求
     * @param url
     * @return
     * @throws IOException
     */
    public static String getService(String url) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Authorization", "Basic TVkzMDAyMDM6SFZneHVpa3ZiWUpSZEN0RzN6QkFSd2dHTlNRVFNweVtwd3NLYm1HUg==");
        ResponseHandler<String> responseHandler = response -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("数据发送异常: " + status);
            }
        };
        String responseBody = httpclient.execute(httpget, responseHandler);
        return responseBody;
    }

    /**
     * post请求
     * @param object
     * @throws IOException
     */
    public static void postService(String url, JSONObject object) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.addHeader("Authorization", "Basic TVkzMDAyMDM6SFZneHVpa3ZiWUpSZEN0RzN6QkFSd2dHTlNRVFNweVtwd3NLYm1HUg==");
        // 解决中文乱码问题
        JSONObject jsonObject = object;
        StringEntity stringEntity = new StringEntity(jsonObject.toString(), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        httpPost.setEntity(stringEntity);
        /* System.out.println("Executing request " + httpPost.getRequestLine()); **/
        ResponseHandler<String> responseHandler = response -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("数据发送异常: " + status);
            }
        };
        httpclient.execute(httpPost, responseHandler);
    }

    public static void deleteService(String url) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete httpDelete = new HttpDelete(url);
        ResponseHandler<String> responseHandler = response -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("数据发送异常: " + status);
            }
        };
        httpclient.execute(httpDelete, responseHandler);
    }
}
